class Document < ActiveRecord::Base
  belongs_to :user
  has_many :comments
  has_one :job
  has_many :annotations
  #ajaxful star rating
  ajaxful_rateable :stars => 5, :dimensions => [:discussable], :allowupdate => true
  #tag cloud generation
  acts_as_taggable
  #paperclip attachment
  has_attached_file :docfile, :path => Proc.new {":rails_root/public/mydocuments/:id/#{self.generate_random_string}.:extension"}
                    #:styles => {:thumb => ["32x32#", :png]}
                    #:url => "/assets/documents:id/:style.:extension",
                    #

  def self.generate_random_string(length=6)
    string = ""
    chars = ("A".."Z").to_a
    length.times do
      string << chars[rand(chars.length-1)]
    end
    string
    
  end

  def male_annotators
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:gender => "Male"}})
  end

  def female_annotators
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:gender => "Female"}})
  end

  def annotation_by_orientation(orientation)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:orientation => orientation}})
  end

  def annotation_by_education(education)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:education => education}})
  end

  def annotation_by_race(race)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:race => race}})
  end

  def annotation_by_religion(religion)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:religious_view => religion}})
  end

  def annotation_by_political(political)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:political_view => political}})
  end

  def annotation_by_occupation(occupation)
    Annotation.find(:all, :select => "DISTINCT user_id", :joins => :user,
      :conditions => {:document_id => self.id, :users => {:occupation => occupation}})
  end

  
  

end
